/**
 * 文  件  名：  RemindInfoDaoImpl.java
 * 工  程  名：  7You
 * 创建日期：  2013-10-14
 * 创建作者：  杨  强
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IRemindInfoDao;
import com.qiyou.entity.RemindInfo;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * 提醒消息数据操作实现类
 * 
 * @author 杨 强
 * 
 */
public class RemindInfoDaoImpl implements IRemindInfoDao {
    private Connection conn = null;

    public RemindInfoDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(RemindInfo remindInfo) throws SQLException {
        boolean result = false;
        String sql = "insert into `tb_remind_info`(`userID`, `riType`, `msgID`, `riState`, `riDate`, `riPost`) values(?, ?, ?, ?, ?)";
        Object[] params = TransformUtil.beanTOArray(remindInfo, true);
        params[4] = new Timestamp(remindInfo.getDate().getTime());
        result = DBUtil.execute(conn, sql, params);
        return result;
    }

    @Override
    public boolean doUpdate(RemindInfo remindInfo) throws SQLException {
        boolean result = false;
        String sql = "update `tb_remind_info` set `riID` = ?, `userID` = ?, `riType` = ?, `msgID` = ?, `riState` = ?, `riDate` = ?, `riPost` = ? where `riID` = ?";
        Object[] params = TransformUtil.beanTOArray(remindInfo, false);
        params[5] = new Timestamp(remindInfo.getDate().getTime());
        result = DBUtil.execute(conn, sql, params, params[0]);
        return result;
    }

    @Override
    public boolean doDelete(long remindInfoID) throws SQLException {
        boolean result = false;
        String sql = "delete from `tb_remind_info` where `riID` = ?";
        result = DBUtil.execute(conn, sql, remindInfoID);
        return result;
    }

    @Override
    public RemindInfo getRemindInfo(long remindInfoID) throws SQLException {
        RemindInfo result = null;
        String sql = "select * from `tb_remind_info` where `riID` = ?";
        List<Object[]> results = DBUtil.query(conn, sql, remindInfoID);
        if (null != results && results.size() > 0) {
            result = (RemindInfo) TransformUtil.arrayToBean(results.get(0),
                    RemindInfo.class);
        }
        return result;
    }

    @Override
    public List<RemindInfo> getRemindInfos(String userID) throws SQLException {
        List<RemindInfo> result = null;
        String sql = "select * from `tb_remind_info` where `userID` = ?";
        List<Object[]> results = DBUtil.query(conn, sql, userID);
        if (null != results && results.size() > 0) {
            result = new ArrayList<RemindInfo>(results.size());
            RemindInfo remindInfo = null;
            for (Object[] objects : results) {
                remindInfo = (RemindInfo) TransformUtil.arrayToBean(objects,
                        RemindInfo.class);
                result.add(remindInfo);
            }
        }
        return result;
    }

    @Override
    public List<RemindInfo> getRemindInfos(Object... params)
            throws SQLException {
        List<RemindInfo> result = null;
        StringBuffer sql = new StringBuffer("select * from `tb_remind_info` ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (null != all && all.size() > 0) {
            result = new ArrayList<RemindInfo>(all.size());
            RemindInfo remindInfo = null;
            for (Object[] objects : all) {
                remindInfo = (RemindInfo) TransformUtil.arrayToBean(objects,
                        RemindInfo.class);
                result.add(remindInfo);
            }
        }

        return result;
    }

}
